<?php
/*-------------------------------------------------------------------------\
|
|  ========================================================
|  Fusion Registry GPL
|  Copyright (C) 2007 Fusion Scripts
|
|  This program is free software; you can redistribute it and/or
|  modify it under the terms of the GNU General Public License
|  as published by the Free Software Foundation; either version 2
|  of the License, or (at your option) any later version.
|
|  This program is distributed in the hope that it will be useful,
|  but WITHOUT ANY WARRANTY; without even the implied warranty of
|  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
|  GNU General Public License for more details.
|
|  You should have received a copy of the GNU General Public License
|  along with this program; if not, write to the Free Software
|  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
|  ========================================================
|
|  Email: info@fusionscripts.com
|  Web-Site: http://www.fusionscripts.com/
|
|  $Id: instagrade.php 4 2008-08-03 01:43:03Z registry.fusion $
|
\-------------------------------------------------------------------------*/

/**
 * Upgrade system
 *
 * @package Fusion Registry
 * @subpackage upgrader
 * @version $Id: instagrade.php 4 2008-08-03 01:43:03Z registry.fusion $
 */
class instagrade
{
    var $ipsclass;

    var $version_history;
    var $current_version;
    var $upgrade_kits;
    var $last_upgrade_kit;
    var $current_upgrade;
    var $modules_to_run;


    /**
     * What upgrade kits are available?
     */
    function get_upgrade_kits()
    {
        $return = array();

        $dh = opendir( './' );

        while ( $file = readdir( $dh ) )
        {
            if ( is_dir( './'.$file ) )
            {
                if ( $file != "." && $file != ".." )
                {
                    if ( strstr( $file, 'upgrade_' ) )
                    {
                        $tmp = str_replace( "upgrade_", "", $file );
                        $return[ $tmp ] = $tmp;
                    }
                }
            }
        }

        closedir( $dh );

        sort($return);

        $this->upgrade_kits = $return;
    }


    /**
     * Get current version installed (if possible) and the next upgrade version
     */
    function get_current_version()
    {
        $this->ipsclass->DB->return_die = 1;

        $row = array();

        // Let's first try and see if they have a recorded version
        if ( $this->ipsclass->DB->query("SELECT config_value FROM ibf_registry_config WHERE config_name = 'version_long'") )
        {
            $row = $this->ipsclass->DB->fetch_row();
        }
        else
        {
            // Reset fail indicators
            $this->ipsclass->DB->error      = "";
            $this->ipsclass->DB->return_die = 0;
            $this->ipsclass->DB->error_no   = 0;
            $this->ipsclass->DB->failed     = 0;
        }

        if ( !empty($row['config_value']) )
        {
            $this->current_version = $row['config_value'];
        }
        elseif ( $this->ipsclass->DB->field_exists( 'featured_vehicle_random', 'garage_config' ) )
        {
            // They appear to have at least Garage Module 2.0.0 RC4
            $this->current_version = '20007';
        }
        elseif ( $this->ipsclass->DB->field_exists( 'enable_guestbooks', 'garage_config' ) )
        {
            // They appear to have at least Garage Module 1.2.0
            $this->current_version = '10200';
        }

        $this->ipsclass->DB->return_die = 0;

        // Get the next upgrade version
		ksort( $this->upgrade_kits );

		foreach( $this->upgrade_kits as $k => $v )
		{
			if ( $v > $this->current_version )
			{
				if ( ! $this->current_upgrade )
				{
					$this->current_upgrade = $v;
				}

				$this->modules_to_run[] = $this->version_history[ $v ];
			}
		}
    }


    /**
     * What is the newest upgrade kit available?
     */
    function get_latest_upgrade_kit()
    {
        // PHP 5.0.5 problem
        $tmp_var = array_reverse($this->upgrade_kits);

        $this->last_upgrade_kit = array_shift( $tmp_var );
    }


    /**
     * Prints the HTML redirect page
     */
	function redirect($url, $text, $time=2)
	{
        $this->ipsclass->template->print_header( array( 'current_step' => 'Redirecting...' ) );

        $this->ipsclass->template->contents .= "<meta http-equiv='refresh' content=\"{$time}; url={$url}\">
										  <div align='center'>$text<br /><br /><a href='{$url}'>Click here if not redirected...</a></div>";
		
		$this->ipsclass->template->output();
	}


    /**
     * Upgrade System Intro
     */
    function upgrade_system_intro()
    {
        $this->ipsclass->template->print_header();

        $this->ipsclass->template->contents .= <<<HTML
Welcome to the Fusion Registry Upgrade System!
<br />
<br />This system will step you through all the upgrade kits needed to bring your Fusion Registry up-to-date.
<br />
<br />
<div style='color:red;font-weight:bold;' align='center'>Please make a full backup of your database before continuing!</div>
<br />
<br /><div align='center'><a href="index.php?act=kit" class='button' style='color: #003366'>Continue >></a></div>
HTML;

		$this->ipsclass->template->output();
    }


    /**
     * Are we done yet?
     */
    function upgrade_kit_done()
    {
		if ( intval( $this->last_upgrade_kit ) != intval( $this->current_version ) )
		{
			$this->redirect( "index.php?act=kit", "Upgrade kit complete, moving on to the next upgrade kit..." );
		}
		else
		{
            $this->redirect( "index.php?act=finish", "Upgrade complete..." );
		}	
    }


    /**
     * Yes we are all done :)
     */
    function upgrade_finished()
    {
        // Lock the upgrader
        if ( $FH = @fopen( './upgrade.lock', 'w' ) )
        {
            @fwrite( $FH, 'No Soup For You!', 16 );
            @fclose( $FH );
            
            @chmod( './upgrade.lock', 0666 );

            $msg = "<b>Fusion Registry Upgrade Complete!</b><br /><br />".
                   "Although the upgrader is now locked, for added security, please remove the upgrade directory before continuing:<br />'".getcwd()."'<br /><br /><br />".
                   "<div align='center'><a href='".ROOT_PATH."/admin.php?section=components&act=registry&menu=config' target='_blank' class='button' style='color: #003366'>Login to ACP to Configure >></a></div>";
        }
        else
        {
            $msg = "<b>Fusion Registry Upgrade Complete!</b><br /><br />".
                   "PLEASE REMOVE THE UPGRADE DIRECTORY '".getcwd()."BEFORE CONTINUING!<br>Failure to do so will enable ANYONE to delete your Fusion Registry tables at any time!<br /><br /><br />".
                   "<div align='center'><a href='".ROOT_PATH."/admin.php?section=components&act=registry&menu=config' target='_blank' class='button' style='color: #003366'>Login to ACP to Configure >></a></div>";
        }

        $this->ipsclass->template->print_header( array( 'current_step' => 'Upgrade Complete!' ) );

        $this->ipsclass->template->contents .= $msg;

		$this->ipsclass->template->output();
    }
}
?>